3 * Reader for XMP data containing properties relevant to images.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use Psr\Log\LoggerAwareInterface
;
25 use Psr\Log\LoggerInterface
;
26 use Psr\Log\NullLogger
;
27 use Wikimedia\ScopedCallback
;
30 * Class for reading xmp data containing properties relevant to
31 * images, and spitting out an array that FormatMetadata accepts.
33 * Note, this is not meant to recognize every possible thing you can
34 * encode in XMP. It should recognize all the properties we want.
35 * For example it doesn't have support for structures with multiple
36 * nesting levels, as none of the properties we're supporting use that
37 * feature. If it comes across properties it doesn't recognize, it should
40 * The public methods one would call in this class are
42 * Reads in xmp content.
43 * Can potentially be called multiple times with partial data each time.
44 * - parseExtended( $content )
45 * Reads XMPExtended blocks (jpeg files only).
47 * Outputs a results array.
49 * Note XMP kind of looks like rdf. They are not the same thing - XMP is
50 * encoded as a specific subset of rdf. This class can read XMP. It cannot
53 class XMPReader
implements LoggerAwareInterface
{
54 /** @var array XMP item configuration array */
57 /** @var array Array to hold the current element (and previous element, and so on) */
58 private $curItem = [];
60 /** @var bool|string The structure name when processing nested structures. */
61 private $ancestorStruct = false;
63 /** @var bool|string Temporary holder for character data that appears in xmp doc. */
64 private $charContent = false;
66 /** @var array Stores the state the xmpreader is in (see MODE_FOO constants) */
69 /** @var array Array to hold results */
70 private $results = [];
72 /** @var bool If we're doing a seq or bag. */
73 private $processingArray = false;
75 /** @var bool|string Used for lang alts only */
76 private $itemLang = false;
78 /** @var resource A resource handle for the XML parser */
81 /** @var bool|string Character set like 'UTF-8' */
82 private $charset = false;
85 private $extendedXMPOffset = 0;
87 /** @var int Flag determining if the XMP is safe to parse **/
88 private $parsable = 0;
90 /** @var string Buffer of XML to parse **/
91 private $xmlParsableBuffer = '';
94 * These are various mode constants.
95 * they are used to figure out what to do
96 * with an element when its encountered.
98 * For example, MODE_IGNORE is used when processing
99 * a property we're not interested in. So if a new
100 * element pops up when we're in that mode, we ignore it.
102 const MODE_INITIAL
= 0;
103 const MODE_IGNORE
= 1;
105 const MODE_LI_LANG
= 3;
106 const MODE_QDESC
= 4;
108 // The following MODE constants are also used in the
109 // $items array to denote what type of property the item is.
110 const MODE_SIMPLE
= 10;
111 const MODE_STRUCT
= 11; // structure (associative array)
112 const MODE_SEQ
= 12; // ordered list
113 const MODE_BAG
= 13; // unordered list
114 const MODE_LANG
= 14;
115 const MODE_ALT
= 15; // non-language alt. Currently not implemented, and not needed atm.
116 const MODE_BAGSTRUCT
= 16; // A BAG of Structs.
118 const NS_RDF
= 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
119 const NS_XML
= 'http://www.w3.org/XML/1998/namespace';
121 // States used while determining if XML is safe to parse
122 const PARSABLE_UNKNOWN
= 0;
123 const PARSABLE_OK
= 1;
124 const PARSABLE_BUFFERING
= 2;
125 const PARSABLE_NO
= 3;
128 * @var LoggerInterface
138 * Primary job is to initialize the XMLParser
139 * @param LoggerInterface|null $logger
140 * @param string $filename
142 function __construct( LoggerInterface
$logger = null, $filename = 'unknown' ) {
143 if ( !function_exists( 'xml_parser_create_ns' ) ) {
144 // this should already be checked by this point
145 throw new RuntimeException( 'XMP support requires XML Parser' );
148 $this->setLogger( $logger );
150 $this->setLogger( new NullLogger() );
152 $this->filename
= $filename;
154 $this->items
= XMPInfo
::getItems();
156 $this->resetXMLParser();
159 public function setLogger( LoggerInterface
$logger ) {
160 $this->logger
= $logger;
164 * free the XML parser.
166 * @note It is unclear to me if we really need to do this ourselves
167 * or if php garbage collection will automatically free the xmlParser
168 * when it is no longer needed.
170 private function destroyXMLParser() {
171 if ( $this->xmlParser
) {
172 xml_parser_free( $this->xmlParser
);
173 $this->xmlParser
= null;
178 * Main use is if a single item has multiple xmp documents describing it.
179 * For example in jpeg's with extendedXMP
181 private function resetXMLParser() {
182 $this->destroyXMLParser();
184 $this->xmlParser
= xml_parser_create_ns( 'UTF-8', ' ' );
185 xml_parser_set_option( $this->xmlParser
, XML_OPTION_CASE_FOLDING
, 0 );
186 xml_parser_set_option( $this->xmlParser
, XML_OPTION_SKIP_WHITE
, 1 );
188 xml_set_element_handler( $this->xmlParser
,
189 [ $this, 'startElement' ],
190 [ $this, 'endElement' ] );
192 xml_set_character_data_handler( $this->xmlParser
, [ $this, 'char' ] );
194 $this->parsable
= self
::PARSABLE_UNKNOWN
;
195 $this->xmlParsableBuffer
= '';
199 * Check if this instance supports using this class
202 public static function isSupported() {
203 return function_exists( 'xml_parser_create_ns' ) && class_exists( 'XMLReader' );
206 /** Get the result array. Do some post-processing before returning
207 * the array, and transform any metadata that is special-cased.
209 * @return array Array of results as an array of arrays suitable for
210 * FormatMetadata::getFormattedData().
212 public function getResults() {
213 // xmp-special is for metadata that affects how stuff
214 // is extracted. For example xmpNote:HasExtendedXMP.
216 // It is also used to handle photoshop:AuthorsPosition
217 // which is weird and really part of another property,
218 // see 2:85 in IPTC. See also pg 21 of IPTC4XMP standard.
219 // The location fields also use it.
221 $data = $this->results
;
223 if ( isset( $data['xmp-special']['AuthorsPosition'] )
224 && is_string( $data['xmp-special']['AuthorsPosition'] )
225 && isset( $data['xmp-general']['Artist'][0] )
227 // Note, if there is more than one creator,
228 // this only applies to first. This also will
229 // only apply to the dc:Creator prop, not the
232 $data['xmp-general']['Artist'][0] =
233 $data['xmp-special']['AuthorsPosition'] . ', '
234 . $data['xmp-general']['Artist'][0];
237 // Go through the LocationShown and LocationCreated
238 // changing it to the non-hierarchal form used by
239 // the other location fields.
241 if ( isset( $data['xmp-special']['LocationShown'][0] )
242 && is_array( $data['xmp-special']['LocationShown'][0] )
244 // the is_array is just paranoia. It should always
246 foreach ( $data['xmp-special']['LocationShown'] as $loc ) {
247 if ( !is_array( $loc ) ) {
248 // To avoid copying over the _type meta-fields.
251 foreach ( $loc as $field => $val ) {
252 $data['xmp-general'][$field . 'Dest'][] = $val;
256 if ( isset( $data['xmp-special']['LocationCreated'][0] )
257 && is_array( $data['xmp-special']['LocationCreated'][0] )
259 // the is_array is just paranoia. It should always
261 foreach ( $data['xmp-special']['LocationCreated'] as $loc ) {
262 if ( !is_array( $loc ) ) {
263 // To avoid copying over the _type meta-fields.
266 foreach ( $loc as $field => $val ) {
267 $data['xmp-general'][$field . 'Created'][] = $val;
272 // We don't want to return the special values, since they're
273 // special and not info to be stored about the file.
274 unset( $data['xmp-special'] );
276 // Convert GPSAltitude to negative if below sea level.
277 if ( isset( $data['xmp-exif']['GPSAltitudeRef'] )
278 && isset( $data['xmp-exif']['GPSAltitude'] )
280 // Must convert to a real before multiplying by -1
281 // XMPValidate guarantees there will always be a '/' in this value.
282 list( $nom, $denom ) = explode( '/', $data['xmp-exif']['GPSAltitude'] );
283 $data['xmp-exif']['GPSAltitude'] = $nom / $denom;
285 if ( $data['xmp-exif']['GPSAltitudeRef'] == '1' ) {
286 $data['xmp-exif']['GPSAltitude'] *= -1;
288 unset( $data['xmp-exif']['GPSAltitudeRef'] );
295 * Main function to call to parse XMP. Use getResults to
298 * Also catches any errors during processing, writes them to
299 * debug log, blanks result array and returns false.
301 * @param string $content XMP data
302 * @param bool $allOfIt If this is all the data (true) or if its split up (false). Default true
303 * @throws RuntimeException
304 * @return bool Success.
306 public function parse( $content, $allOfIt = true ) {
307 if ( !$this->xmlParser
) {
308 $this->resetXMLParser();
312 // detect encoding by looking for BOM which is supposed to be in processing instruction.
313 // see page 12 of http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart3.pdf
314 if ( !$this->charset
) {
316 if ( preg_match( '/\xEF\xBB\xBF|\xFE\xFF|\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\xFF\xFE/',
321 $this->charset
= 'UTF-16BE';
324 $this->charset
= 'UTF-16LE';
326 case "\x00\x00\xFE\xFF":
327 $this->charset
= 'UTF-32BE';
329 case "\xFF\xFE\x00\x00":
330 $this->charset
= 'UTF-32LE';
333 $this->charset
= 'UTF-8';
336 // this should be impossible to get to
337 throw new RuntimeException( "Invalid BOM" );
340 // standard specifically says, if no bom assume utf-8
341 $this->charset
= 'UTF-8';
344 if ( $this->charset
!== 'UTF-8' ) {
345 // don't convert if already utf-8
346 Wikimedia\
suppressWarnings();
347 $content = iconv( $this->charset
, 'UTF-8//IGNORE', $content );
348 Wikimedia\restoreWarnings
();
351 // Ensure the XMP block does not have an xml doctype declaration, which
352 // could declare entities unsafe to parse with xml_parse (T85848/T71210).
353 if ( $this->parsable
!== self
::PARSABLE_OK
) {
354 if ( $this->parsable
=== self
::PARSABLE_NO
) {
355 throw new RuntimeException( 'Unsafe doctype declaration in XML.' );
358 $content = $this->xmlParsableBuffer
. $content;
359 if ( !$this->checkParseSafety( $content ) ) {
360 if ( !$allOfIt && $this->parsable
!== self
::PARSABLE_NO
) {
361 // parse wasn't Unsuccessful yet, so return true
365 $msg = ( $this->parsable
=== self
::PARSABLE_NO
) ?
366 'Unsafe doctype declaration in XML.' :
367 'No root element found in XML.';
368 throw new RuntimeException( $msg );
372 $ok = xml_parse( $this->xmlParser
, $content, $allOfIt );
374 $code = xml_get_error_code( $this->xmlParser
);
375 $error = xml_error_string( $code );
376 $line = xml_get_current_line_number( $this->xmlParser
);
377 $col = xml_get_current_column_number( $this->xmlParser
);
378 $offset = xml_get_current_byte_index( $this->xmlParser
);
381 '{method} : Error reading XMP content: {error} ' .
382 '(file: {file}, line: {line} column: {column} ' .
383 'byte offset: {offset})',
385 'method' => __METHOD__
,
386 'error_code' => $code,
388 'file' => $this->filename
,
392 'content' => $content,
394 $this->results
= []; // blank if error.
395 $this->destroyXMLParser();
398 } catch ( Exception
$e ) {
399 $this->logger
->warning(
400 '{method} {exception}',
402 'method' => __METHOD__
,
404 'file' => $this->filename
,
405 'content' => $content,
412 $this->destroyXMLParser();
418 /** Entry point for XMPExtended blocks in jpeg files
420 * @todo In serious need of testing
421 * @see http://www.adobe.ge/devnet/xmp/pdfs/XMPSpecificationPart3.pdf XMP spec part 3 page 20
422 * @param string $content XMPExtended block minus the namespace signature
423 * @return bool If it succeeded.
425 public function parseExtended( $content ) {
426 // @todo FIXME: This is untested. Hard to find example files
427 // or programs that make such files..
428 $guid = substr( $content, 0, 32 );
429 if ( !isset( $this->results
['xmp-special']['HasExtendedXMP'] )
430 ||
$this->results
['xmp-special']['HasExtendedXMP'] !== $guid
432 $this->logger
->info( __METHOD__
.
433 " Ignoring XMPExtended block due to wrong guid (guid= '{guid}')",
436 'file' => $this->filename
,
442 $len = unpack( 'Nlength/Noffset', substr( $content, 32, 8 ) );
445 $len['length'] < 4 ||
446 $len['offset'] < 0 ||
447 $len['offset'] > $len['length']
450 __METHOD__
. 'Error reading extended XMP block, invalid length or offset.',
451 [ 'file' => $this->filename
]
457 // we're not very robust here. we should accept it in the wrong order.
458 // To quote the XMP standard:
459 // "A JPEG writer should write the ExtendedXMP marker segments in order,
460 // immediately following the StandardXMP. However, the JPEG standard
461 // does not require preservation of marker segment order. A robust JPEG
462 // reader should tolerate the marker segments in any order."
463 // On the other hand, the probability that an image will have more than
464 // 128k of metadata is rather low... so the probability that it will have
465 // > 128k, and be in the wrong order is very low...
467 if ( $len['offset'] !== $this->extendedXMPOffset
) {
468 $this->logger
->info( __METHOD__
. 'Ignoring XMPExtended block due to wrong order. (Offset was '
469 . $len['offset'] . ' but expected ' . $this->extendedXMPOffset
. ')',
470 [ 'file' => $this->filename
]
476 if ( $len['offset'] === 0 ) {
477 // if we're starting the extended block, we've probably already
478 // done the XMPStandard block, so reset.
479 $this->resetXMLParser();
482 $this->extendedXMPOffset +
= $len['length'];
484 $actualContent = substr( $content, 40 );
486 if ( $this->extendedXMPOffset
=== strlen( $actualContent ) ) {
492 $this->logger
->debug(
493 __METHOD__
. 'Parsing a XMPExtended block',
494 [ 'file' => $this->filename
]
497 return $this->parse( $actualContent, $atEnd );
501 * Character data handler
502 * Called whenever character data is found in the xmp document.
504 * does nothing if we're in MODE_IGNORE or if the data is whitespace
505 * throws an error if we're not in MODE_SIMPLE (as we're not allowed to have character
506 * data in the other modes).
508 * As an example, this happens when we encounter XMP like:
509 * <exif:DigitalZoomRatio>0/10</exif:DigitalZoomRatio>
510 * and are processing the 0/10 bit.
512 * @param resource $parser XMLParser reference to the xml parser
513 * @param string $data Character data
514 * @throws RuntimeException On invalid data
516 function char( $parser, $data ) {
517 $data = trim( $data );
518 if ( trim( $data ) === "" ) {
522 if ( !isset( $this->mode
[0] ) ) {
523 throw new RuntimeException( 'Unexpected character data before first rdf:Description element' );
526 if ( $this->mode
[0] === self
::MODE_IGNORE
) {
530 if ( $this->mode
[0] !== self
::MODE_SIMPLE
531 && $this->mode
[0] !== self
::MODE_QDESC
533 throw new RuntimeException( 'character data where not expected. (mode ' . $this->mode
[0] . ')' );
536 // to check, how does this handle w.s.
537 if ( $this->charContent
=== false ) {
538 $this->charContent
= $data;
540 $this->charContent
.= $data;
545 * Check if a block of XML is safe to pass to xml_parse, i.e. doesn't
546 * contain a doctype declaration which could contain a dos attack if we
547 * parse it and expand internal entities (T85848).
549 * @param string $content xml string to check for parse safety
550 * @return bool true if the xml is safe to parse, false otherwise
552 private function checkParseSafety( $content ) {
553 $reader = new XMLReader();
556 // For XMLReader to parse incomplete/invalid XML, it has to be open()'ed
557 // instead of using XML().
559 'data://text/plain,' . urlencode( $content ),
561 LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET
564 $oldDisable = libxml_disable_entity_loader( true );
565 /** @noinspection PhpUnusedLocalVariableInspection */
566 $reset = new ScopedCallback(
567 'libxml_disable_entity_loader',
570 $reader->setParserProperty( XMLReader
::SUBST_ENTITIES
, false );
572 // Even with LIBXML_NOWARNING set, XMLReader::read gives a warning
573 // when parsing truncated XML, which causes unit tests to fail.
574 Wikimedia\
suppressWarnings();
575 while ( $reader->read() ) {
576 if ( $reader->nodeType
=== XMLReader
::ELEMENT
) {
577 // Reached the first element without hitting a doctype declaration
578 $this->parsable
= self
::PARSABLE_OK
;
582 if ( $reader->nodeType
=== XMLReader
::DOC_TYPE
) {
583 $this->parsable
= self
::PARSABLE_NO
;
588 Wikimedia\restoreWarnings
();
590 if ( !is_null( $result ) ) {
594 // Reached the end of the parsable xml without finding an element
595 // or doctype. Buffer and try again.
596 $this->parsable
= self
::PARSABLE_BUFFERING
;
597 $this->xmlParsableBuffer
= $content;
601 /** When we hit a closing element in MODE_IGNORE
602 * Check to see if this is the element we started to ignore,
603 * in which case we get out of MODE_IGNORE
605 * @param string $elm Namespace of element followed by a space and then tag name of element.
607 private function endElementModeIgnore( $elm ) {
608 if ( $this->curItem
[0] === $elm ) {
609 array_shift( $this->curItem
);
610 array_shift( $this->mode
);
615 * Hit a closing element when in MODE_SIMPLE.
616 * This generally means that we finished processing a
617 * property value, and now have to save the result to the
620 * For example, when processing:
621 * <exif:DigitalZoomRatio>0/10</exif:DigitalZoomRatio>
622 * this deals with when we hit </exif:DigitalZoomRatio>.
624 * Or it could be if we hit the end element of a property
625 * of a compound data structure (like a member of an array).
627 * @param string $elm Namespace, space, and tag name.
629 private function endElementModeSimple( $elm ) {
630 if ( $this->charContent
!== false ) {
631 if ( $this->processingArray
) {
632 // if we're processing an array, use the original element
633 // name instead of rdf:li.
634 list( $ns, $tag ) = explode( ' ', $this->curItem
[0], 2 );
636 list( $ns, $tag ) = explode( ' ', $elm, 2 );
638 $this->saveValue( $ns, $tag, $this->charContent
);
640 $this->charContent
= false; // reset
642 array_shift( $this->curItem
);
643 array_shift( $this->mode
);
647 * Hit a closing element in MODE_STRUCT, MODE_SEQ, MODE_BAG
648 * generally means we've finished processing a nested structure.
649 * resets some internal variables to indicate that.
651 * Note this means we hit the closing element not the "</rdf:Seq>".
653 * @par For example, when processing:
655 * <exif:ISOSpeedRatings> <rdf:Seq> <rdf:li>64</rdf:li>
656 * </rdf:Seq> </exif:ISOSpeedRatings>
659 * This method is called when we hit the "</exif:ISOSpeedRatings>" tag.
661 * @param string $elm Namespace . space . tag name.
662 * @throws RuntimeException
664 private function endElementNested( $elm ) {
665 /* cur item must be the same as $elm, unless if in MODE_STRUCT
666 * in which case it could also be rdf:Description */
667 if ( $this->curItem
[0] !== $elm
668 && !( $elm === self
::NS_RDF
. ' Description'
669 && $this->mode
[0] === self
::MODE_STRUCT
)
671 throw new RuntimeException( "nesting mismatch. got a </$elm> but expected a </" .
672 $this->curItem
[0] . '>' );
675 // Validate structures.
676 list( $ns, $tag ) = explode( ' ', $elm, 2 );
677 if ( isset( $this->items
[$ns][$tag]['validate'] ) ) {
678 $info =& $this->items
[$ns][$tag];
679 $finalName = $info['map_name'] ??
$tag;
681 if ( is_array( $info['validate'] ) ) {
682 $validate = $info['validate'];
684 $validator = new XMPValidate( $this->logger
);
685 $validate = [ $validator, $info['validate'] ];
688 if ( !isset( $this->results
['xmp-' . $info['map_group']][$finalName] ) ) {
689 // This can happen if all the members of the struct failed validation.
690 $this->logger
->debug(
691 __METHOD__
. " <$ns:$tag> has no valid members.",
692 [ 'file' => $this->filename
]
694 } elseif ( is_callable( $validate ) ) {
695 $val =& $this->results
['xmp-' . $info['map_group']][$finalName];
696 call_user_func_array( $validate, [ $info, &$val, false ] );
697 if ( is_null( $val ) ) {
698 // the idea being the validation function will unset the variable if
701 __METHOD__
. " <$ns:$tag> failed validation.",
702 [ 'file' => $this->filename
]
704 unset( $this->results
['xmp-' . $info['map_group']][$finalName] );
707 $this->logger
->warning(
708 __METHOD__
. " Validation function for $finalName (" .
709 $validate[0] . '::' . $validate[1] . '()) is not callable.',
710 [ 'file' => $this->filename
]
715 array_shift( $this->curItem
);
716 array_shift( $this->mode
);
717 $this->ancestorStruct
= false;
718 $this->processingArray
= false;
719 $this->itemLang
= false;
723 * Hit a closing element in MODE_LI (either rdf:Seq, or rdf:Bag )
724 * Add information about what type of element this is.
726 * Note we still have to hit the outer "</property>"
728 * @par For example, when processing:
730 * <exif:ISOSpeedRatings> <rdf:Seq> <rdf:li>64</rdf:li>
731 * </rdf:Seq> </exif:ISOSpeedRatings>
734 * This method is called when we hit the "</rdf:Seq>".
735 * (For comparison, we call endElementModeSimple when we
736 * hit the "</rdf:li>")
738 * @param string $elm Namespace . ' ' . element name
739 * @throws RuntimeException
741 private function endElementModeLi( $elm ) {
742 list( $ns, $tag ) = explode( ' ', $this->curItem
[0], 2 );
743 $info = $this->items
[$ns][$tag];
744 $finalName = $info['map_name'] ??
$tag;
746 array_shift( $this->mode
);
748 if ( !isset( $this->results
['xmp-' . $info['map_group']][$finalName] ) ) {
749 $this->logger
->debug(
750 __METHOD__
. " Empty compund element $finalName.",
751 [ 'file' => $this->filename
]
757 if ( $elm === self
::NS_RDF
. ' Seq' ) {
758 $this->results
['xmp-' . $info['map_group']][$finalName]['_type'] = 'ol';
759 } elseif ( $elm === self
::NS_RDF
. ' Bag' ) {
760 $this->results
['xmp-' . $info['map_group']][$finalName]['_type'] = 'ul';
761 } elseif ( $elm === self
::NS_RDF
. ' Alt' ) {
762 // extra if needed as you could theoretically have a non-language alt.
763 if ( $info['mode'] === self
::MODE_LANG
) {
764 $this->results
['xmp-' . $info['map_group']][$finalName]['_type'] = 'lang';
767 throw new RuntimeException(
768 __METHOD__
. " expected </rdf:seq> or </rdf:bag> but instead got $elm."
774 * End element while in MODE_QDESC
775 * mostly when ending an element when we have a simple value
776 * that has qualifiers.
778 * Qualifiers aren't all that common, and we don't do anything
781 * @param string $elm Namespace and element
783 private function endElementModeQDesc( $elm ) {
784 if ( $elm === self
::NS_RDF
. ' value' ) {
785 list( $ns, $tag ) = explode( ' ', $this->curItem
[0], 2 );
786 $this->saveValue( $ns, $tag, $this->charContent
);
790 array_shift( $this->mode
);
791 array_shift( $this->curItem
);
796 * Handler for hitting a closing element.
798 * generally just calls a helper function depending on what
801 * Ignores the outer wrapping elements that are optional in
802 * xmp and have no meaning.
804 * @param resource $parser
805 * @param string $elm Namespace . ' ' . element name
806 * @throws RuntimeException
808 function endElement( $parser, $elm ) {
809 if ( $elm === ( self
::NS_RDF
. ' RDF' )
810 ||
$elm === 'adobe:ns:meta/ xmpmeta'
811 ||
$elm === 'adobe:ns:meta/ xapmeta'
817 if ( $elm === self
::NS_RDF
. ' type' ) {
818 // these aren't really supported properly yet.
819 // However, it appears they almost never used.
821 __METHOD__
. ' encountered <rdf:type>',
822 [ 'file' => $this->filename
]
826 if ( strpos( $elm, ' ' ) === false ) {
827 // This probably shouldn't happen.
828 // However, there is a bug in an adobe product
829 // that forgets the namespace on some things.
830 // (Luckily they are unimportant things).
832 __METHOD__
. " Encountered </$elm> which has no namespace. Skipping.",
833 [ 'file' => $this->filename
]
839 if ( count( $this->mode
) === 0 ) {
840 // This should never ever happen and means
841 // there is a pretty major bug in this class.
842 throw new RuntimeException( 'Encountered end element with no mode' );
845 if ( count( $this->curItem
) == 0 && $this->mode
[0] !== self
::MODE_INITIAL
) {
846 // just to be paranoid. Should always have a curItem, except for initially
847 // (aka during MODE_INITAL).
848 throw new RuntimeException( "Hit end element </$elm> but no curItem" );
851 switch ( $this->mode
[0] ) {
852 case self
::MODE_IGNORE
:
853 $this->endElementModeIgnore( $elm );
855 case self
::MODE_SIMPLE
:
856 $this->endElementModeSimple( $elm );
858 case self
::MODE_STRUCT
:
861 case self
::MODE_LANG
:
862 case self
::MODE_BAGSTRUCT
:
863 $this->endElementNested( $elm );
865 case self
::MODE_INITIAL
:
866 if ( $elm === self
::NS_RDF
. ' Description' ) {
867 array_shift( $this->mode
);
869 throw new RuntimeException( 'Element ended unexpectedly while in MODE_INITIAL' );
873 case self
::MODE_LI_LANG
:
874 $this->endElementModeLi( $elm );
876 case self
::MODE_QDESC
:
877 $this->endElementModeQDesc( $elm );
881 __METHOD__
. " no mode (elm = $elm)",
882 [ 'file' => $this->filename
]
889 * Hit an opening element while in MODE_IGNORE
891 * XMP is extensible, so ignore any tag we don't understand.
893 * Mostly ignores, unless we encounter the element that we are ignoring.
894 * in which case we add it to the item stack, so we can ignore things
895 * that are nested, correctly.
897 * @param string $elm Namespace . ' ' . tag name
899 private function startElementModeIgnore( $elm ) {
900 if ( $elm === $this->curItem
[0] ) {
901 array_unshift( $this->curItem
, $elm );
902 array_unshift( $this->mode
, self
::MODE_IGNORE
);
907 * Start element in MODE_BAG (unordered array)
908 * this should always be <rdf:Bag>
910 * @param string $elm Namespace . ' ' . tag
911 * @throws RuntimeException If we have an element that's not <rdf:Bag>
913 private function startElementModeBag( $elm ) {
914 if ( $elm === self
::NS_RDF
. ' Bag' ) {
915 array_unshift( $this->mode
, self
::MODE_LI
);
917 throw new RuntimeException( "Expected <rdf:Bag> but got $elm." );
922 * Start element in MODE_SEQ (ordered array)
923 * this should always be <rdf:Seq>
925 * @param string $elm Namespace . ' ' . tag
926 * @throws RuntimeException If we have an element that's not <rdf:Seq>
928 private function startElementModeSeq( $elm ) {
929 if ( $elm === self
::NS_RDF
. ' Seq' ) {
930 array_unshift( $this->mode
, self
::MODE_LI
);
931 } elseif ( $elm === self
::NS_RDF
. ' Bag' ) {
934 __METHOD__
. ' Expected an rdf:Seq, but got an rdf:Bag. Pretending' .
935 ' it is a Seq, since some buggy software is known to screw this up.',
936 [ 'file' => $this->filename
]
938 array_unshift( $this->mode
, self
::MODE_LI
);
940 throw new RuntimeException( "Expected <rdf:Seq> but got $elm." );
945 * Start element in MODE_LANG (language alternative)
946 * this should always be <rdf:Alt>
948 * This tag tends to be used for metadata like describe this
949 * picture, which can be translated into multiple languages.
951 * XMP supports non-linguistic alternative selections,
952 * which are really only used for thumbnails, which
953 * we don't care about.
955 * @param string $elm Namespace . ' ' . tag
956 * @throws RuntimeException If we have an element that's not <rdf:Alt>
958 private function startElementModeLang( $elm ) {
959 if ( $elm === self
::NS_RDF
. ' Alt' ) {
960 array_unshift( $this->mode
, self
::MODE_LI_LANG
);
962 throw new RuntimeException( "Expected <rdf:Seq> but got $elm." );
967 * Handle an opening element when in MODE_SIMPLE
969 * This should not happen often. This is for if a simple element
970 * already opened has a child element. Could happen for a
974 * <exif:DigitalZoomRatio><rdf:Description><rdf:value>0/10</rdf:value>
975 * <foo:someQualifier>Bar</foo:someQualifier> </rdf:Description>
976 * </exif:DigitalZoomRatio>
978 * This method is called when processing the <rdf:Description> element
980 * @param string $elm Namespace and tag names separated by space.
981 * @param array $attribs Attributes of the element.
982 * @throws RuntimeException
984 private function startElementModeSimple( $elm, $attribs ) {
985 if ( $elm === self
::NS_RDF
. ' Description' ) {
986 // If this value has qualifiers
987 array_unshift( $this->mode
, self
::MODE_QDESC
);
988 array_unshift( $this->curItem
, $this->curItem
[0] );
990 if ( isset( $attribs[self
::NS_RDF
. ' value'] ) ) {
991 list( $ns, $tag ) = explode( ' ', $this->curItem
[0], 2 );
992 $this->saveValue( $ns, $tag, $attribs[self
::NS_RDF
. ' value'] );
994 } elseif ( $elm === self
::NS_RDF
. ' value' ) {
995 // This should not be here.
996 throw new RuntimeException( __METHOD__
. ' Encountered <rdf:value> where it was unexpected.' );
998 // something else we don't recognize, like a qualifier maybe.
999 $this->logger
->info( __METHOD__
.
1000 " Encountered element <{element}> where only expecting character data as value of {curitem}",
1003 'curitem' => $this->curItem
[0],
1004 'file' => $this->filename
,
1007 array_unshift( $this->mode
, self
::MODE_IGNORE
);
1008 array_unshift( $this->curItem
, $elm );
1013 * Start an element when in MODE_QDESC.
1014 * This generally happens when a simple element has an inner
1015 * rdf:Description to hold qualifier elements.
1018 * <exif:DigitalZoomRatio><rdf:Description><rdf:value>0/10</rdf:value>
1019 * <foo:someQualifier>Bar</foo:someQualifier> </rdf:Description>
1020 * </exif:DigitalZoomRatio>
1021 * Called when processing the <rdf:value> or <foo:someQualifier>.
1023 * @param string $elm Namespace and tag name separated by a space.
1025 private function startElementModeQDesc( $elm ) {
1026 if ( $elm === self
::NS_RDF
. ' value' ) {
1027 return; // do nothing
1029 // otherwise its a qualifier, which we ignore
1030 array_unshift( $this->mode
, self
::MODE_IGNORE
);
1031 array_unshift( $this->curItem
, $elm );
1036 * Starting an element when in MODE_INITIAL
1037 * This usually happens when we hit an element inside
1038 * the outer rdf:Description
1040 * This is generally where most properties start.
1042 * @param string $ns Namespace
1043 * @param string $tag Tag name (without namespace prefix)
1044 * @param array $attribs Array of attributes
1045 * @throws RuntimeException
1047 private function startElementModeInitial( $ns, $tag, $attribs ) {
1048 if ( $ns !== self
::NS_RDF
) {
1049 if ( isset( $this->items
[$ns][$tag] ) ) {
1050 if ( isset( $this->items
[$ns][$tag]['structPart'] ) ) {
1051 // If this element is supposed to appear only as
1052 // a child of a structure, but appears here (not as
1053 // a child of a struct), then something weird is
1054 // happening, so ignore this element and its children.
1056 $this->logger
->info(
1057 'Encountered <{element}> outside of its expected parent. Ignoring.',
1058 [ 'element' => "$ns:$tag", 'file' => $this->filename
]
1061 array_unshift( $this->mode
, self
::MODE_IGNORE
);
1062 array_unshift( $this->curItem
, $ns . ' ' . $tag );
1066 $mode = $this->items
[$ns][$tag]['mode'];
1067 array_unshift( $this->mode
, $mode );
1068 array_unshift( $this->curItem
, $ns . ' ' . $tag );
1069 if ( $mode === self
::MODE_STRUCT
) {
1070 $this->ancestorStruct
= $this->items
[$ns][$tag]['map_name'] ??
$tag;
1072 if ( $this->charContent
!== false ) {
1074 // Should not happen in valid XMP.
1075 throw new RuntimeException( 'tag nested in non-whitespace characters.' );
1078 // This element is not on our list of allowed elements so ignore.
1079 $this->logger
->debug( __METHOD__
. ' Ignoring unrecognized element <{element}>.',
1080 [ 'element' => "$ns:$tag", 'file' => $this->filename
] );
1081 array_unshift( $this->mode
, self
::MODE_IGNORE
);
1082 array_unshift( $this->curItem
, $ns . ' ' . $tag );
1087 // process attributes
1088 $this->doAttribs( $attribs );
1092 * Hit an opening element when in a Struct (MODE_STRUCT)
1093 * This is generally for fields of a compound property.
1095 * Example of a struct (abbreviated; flash has more properties):
1097 * <exif:Flash> <rdf:Description> <exif:Fired>True</exif:Fired>
1098 * <exif:Mode>1</exif:Mode></rdf:Description></exif:Flash>
1102 * <exif:Flash rdf:parseType='Resource'> <exif:Fired>True</exif:Fired>
1103 * <exif:Mode>1</exif:Mode></exif:Flash>
1105 * @param string $ns Namespace
1106 * @param string $tag Tag name (no ns)
1107 * @param array $attribs Array of attribs w/ values.
1108 * @throws RuntimeException
1110 private function startElementModeStruct( $ns, $tag, $attribs ) {
1111 if ( $ns !== self
::NS_RDF
) {
1112 if ( isset( $this->items
[$ns][$tag] ) ) {
1113 if ( isset( $this->items
[$ns][$this->ancestorStruct
]['children'] )
1114 && !isset( $this->items
[$ns][$this->ancestorStruct
]['children'][$tag] )
1116 // This assumes that we don't have inter-namespace nesting
1117 // which we don't in all the properties we're interested in.
1118 throw new RuntimeException( " <$tag> appeared nested in <" . $this->ancestorStruct
1119 . "> where it is not allowed." );
1121 array_unshift( $this->mode
, $this->items
[$ns][$tag]['mode'] );
1122 array_unshift( $this->curItem
, $ns . ' ' . $tag );
1123 if ( $this->charContent
!== false ) {
1125 // Should not happen in valid XMP.
1126 throw new RuntimeException( "tag <$tag> nested in non-whitespace characters (" .
1127 $this->charContent
. ")." );
1130 array_unshift( $this->mode
, self
::MODE_IGNORE
);
1131 array_unshift( $this->curItem
, $ns . ' ' . $tag );
1137 if ( $ns === self
::NS_RDF
&& $tag === 'Description' ) {
1138 $this->doAttribs( $attribs );
1139 array_unshift( $this->mode
, self
::MODE_STRUCT
);
1140 array_unshift( $this->curItem
, $this->curItem
[0] );
1145 * opening element in MODE_LI
1146 * process elements of arrays.
1149 * <exif:ISOSpeedRatings> <rdf:Seq> <rdf:li>64</rdf:li>
1150 * </rdf:Seq> </exif:ISOSpeedRatings>
1151 * This method is called when we hit the <rdf:li> element.
1153 * @param string $elm Namespace . ' ' . tagname
1154 * @param array $attribs Attributes. (needed for BAGSTRUCTS)
1155 * @throws RuntimeException If gets a tag other than <rdf:li>
1157 private function startElementModeLi( $elm, $attribs ) {
1158 if ( ( $elm ) !== self
::NS_RDF
. ' li' ) {
1159 throw new RuntimeException( "<rdf:li> expected but got $elm." );
1162 if ( !isset( $this->mode
[1] ) ) {
1163 // This should never ever ever happen. Checking for it
1165 throw new RuntimeException( 'In mode Li, but no 2xPrevious mode!' );
1168 if ( $this->mode
[1] === self
::MODE_BAGSTRUCT
) {
1169 // This list item contains a compound (STRUCT) value.
1170 array_unshift( $this->mode
, self
::MODE_STRUCT
);
1171 array_unshift( $this->curItem
, $elm );
1172 $this->processingArray
= true;
1174 if ( !isset( $this->curItem
[1] ) ) {
1176 throw new RuntimeException( 'Can not find parent of BAGSTRUCT.' );
1178 list( $curNS, $curTag ) = explode( ' ', $this->curItem
[1] );
1179 $this->ancestorStruct
= $this->items
[$curNS][$curTag]['map_name'] ??
$curTag;
1181 $this->doAttribs( $attribs );
1183 // Normal BAG or SEQ containing simple values.
1184 array_unshift( $this->mode
, self
::MODE_SIMPLE
);
1185 // need to add curItem[0] on again since one is for the specific item
1186 // and one is for the entire group.
1187 array_unshift( $this->curItem
, $this->curItem
[0] );
1188 $this->processingArray
= true;
1193 * Opening element in MODE_LI_LANG.
1194 * process elements of language alternatives
1197 * <dc:title> <rdf:Alt> <rdf:li xml:lang="x-default">My house
1198 * </rdf:li> </rdf:Alt> </dc:title>
1200 * This method is called when we hit the <rdf:li> element.
1202 * @param string $elm Namespace . ' ' . tag
1203 * @param array $attribs Array of elements (most importantly xml:lang)
1204 * @throws RuntimeException If gets a tag other than <rdf:li> or if no xml:lang
1206 private function startElementModeLiLang( $elm, $attribs ) {
1207 if ( $elm !== self
::NS_RDF
. ' li' ) {
1208 throw new RuntimeException( __METHOD__
. " <rdf:li> expected but got $elm." );
1210 if ( !isset( $attribs[self
::NS_XML
. ' lang'] )
1211 ||
!preg_match( '/^[-A-Za-z0-9]{2,}$/D', $attribs[self
::NS_XML
. ' lang'] )
1213 throw new RuntimeException( __METHOD__
1214 . " <rdf:li> did not contain, or has invalid xml:lang attribute in lang alternative" );
1217 // Lang is case-insensitive.
1218 $this->itemLang
= strtolower( $attribs[self
::NS_XML
. ' lang'] );
1220 // need to add curItem[0] on again since one is for the specific item
1221 // and one is for the entire group.
1222 array_unshift( $this->curItem
, $this->curItem
[0] );
1223 array_unshift( $this->mode
, self
::MODE_SIMPLE
);
1224 $this->processingArray
= true;
1228 * Hits an opening element.
1229 * Generally just calls a helper based on what MODE we're in.
1230 * Also does some initial set up for the wrapper element
1232 * @param resource $parser
1233 * @param string $elm Namespace "<space>" element
1234 * @param array $attribs Attribute name => value
1235 * @throws RuntimeException
1237 function startElement( $parser, $elm, $attribs ) {
1238 if ( $elm === self
::NS_RDF
. ' RDF'
1239 ||
$elm === 'adobe:ns:meta/ xmpmeta'
1240 ||
$elm === 'adobe:ns:meta/ xapmeta'
1244 } elseif ( $elm === self
::NS_RDF
. ' Description' ) {
1245 if ( count( $this->mode
) === 0 ) {
1247 array_unshift( $this->mode
, self
::MODE_INITIAL
);
1249 } elseif ( $elm === self
::NS_RDF
. ' type' ) {
1250 // This doesn't support rdf:type properly.
1251 // In practise I have yet to see a file that
1252 // uses this element, however it is mentioned
1253 // on page 25 of part 1 of the xmp standard.
1254 // Also it seems as if exiv2 and exiftool do not support
1255 // this either (That or I misunderstand the standard)
1256 $this->logger
->info(
1257 __METHOD__
. ' Encountered <rdf:type> which isn\'t currently supported',
1258 [ 'file' => $this->filename
]
1262 if ( strpos( $elm, ' ' ) === false ) {
1263 // This probably shouldn't happen.
1264 $this->logger
->info(
1265 __METHOD__
. " Encountered <$elm> which has no namespace. Skipping.",
1266 [ 'file' => $this->filename
]
1272 list( $ns, $tag ) = explode( ' ', $elm, 2 );
1274 if ( count( $this->mode
) === 0 ) {
1275 // This should not happen.
1276 throw new RuntimeException( 'Error extracting XMP, '
1277 . "encountered <$elm> with no mode" );
1280 switch ( $this->mode
[0] ) {
1281 case self
::MODE_IGNORE
:
1282 $this->startElementModeIgnore( $elm );
1284 case self
::MODE_SIMPLE
:
1285 $this->startElementModeSimple( $elm, $attribs );
1287 case self
::MODE_INITIAL
:
1288 $this->startElementModeInitial( $ns, $tag, $attribs );
1290 case self
::MODE_STRUCT
:
1291 $this->startElementModeStruct( $ns, $tag, $attribs );
1293 case self
::MODE_BAG
:
1294 case self
::MODE_BAGSTRUCT
:
1295 $this->startElementModeBag( $elm );
1297 case self
::MODE_SEQ
:
1298 $this->startElementModeSeq( $elm );
1300 case self
::MODE_LANG
:
1301 $this->startElementModeLang( $elm );
1303 case self
::MODE_LI_LANG
:
1304 $this->startElementModeLiLang( $elm, $attribs );
1307 $this->startElementModeLi( $elm, $attribs );
1309 case self
::MODE_QDESC
:
1310 $this->startElementModeQDesc( $elm );
1313 throw new RuntimeException( 'StartElement in unknown mode: ' . $this->mode
[0] );
1317 // phpcs:disable Generic.Files.LineLength
1319 * Process attributes.
1320 * Simple values can be stored as either a tag or attribute
1322 * Often the initial "<rdf:Description>" tag just has all the simple
1323 * properties as attributes.
1327 * <rdf:Description rdf:about="" xmlns:exif="http://ns.adobe.com/exif/1.0/" exif:DigitalZoomRatio="0/10">
1330 * @param array $attribs Array attribute=>value
1331 * @throws RuntimeException
1334 private function doAttribs( $attribs ) {
1335 // first check for rdf:parseType attribute, as that can change
1336 // how the attributes are interperted.
1338 if ( isset( $attribs[self
::NS_RDF
. ' parseType'] )
1339 && $attribs[self
::NS_RDF
. ' parseType'] === 'Resource'
1340 && $this->mode
[0] === self
::MODE_SIMPLE
1342 // this is equivalent to having an inner rdf:Description
1343 $this->mode
[0] = self
::MODE_QDESC
;
1345 foreach ( $attribs as $name => $val ) {
1346 if ( strpos( $name, ' ' ) === false ) {
1347 // This shouldn't happen, but so far some old software forgets namespace
1349 $this->logger
->info(
1350 __METHOD__
. ' Encountered non-namespaced attribute: ' .
1351 " $name=\"$val\". Skipping. ",
1352 [ 'file' => $this->filename
]
1356 list( $ns, $tag ) = explode( ' ', $name, 2 );
1357 if ( $ns === self
::NS_RDF
) {
1358 if ( $tag === 'value' ||
$tag === 'resource' ) {
1359 // resource is for url.
1360 // value attribute is a weird way of just putting the contents.
1361 $this->char( $this->xmlParser
, $val );
1363 } elseif ( isset( $this->items
[$ns][$tag] ) ) {
1364 if ( $this->mode
[0] === self
::MODE_SIMPLE
) {
1365 throw new RuntimeException( __METHOD__
1366 . " $ns:$tag found as attribute where not allowed" );
1368 $this->saveValue( $ns, $tag, $val );
1370 $this->logger
->debug(
1371 __METHOD__
. " Ignoring unrecognized element <$ns:$tag>.",
1372 [ 'file' => $this->filename
]
1379 * Given an extracted value, save it to results array
1381 * note also uses $this->ancestorStruct and
1382 * $this->processingArray to determine what name to
1383 * save the value under. (in addition to $tag).
1385 * @param string $ns Namespace of tag this is for
1386 * @param string $tag Tag name
1387 * @param string $val Value to save
1389 private function saveValue( $ns, $tag, $val ) {
1390 $info =& $this->items
[$ns][$tag];
1391 $finalName = $info['map_name'] ??
$tag;
1392 if ( isset( $info['validate'] ) ) {
1393 if ( is_array( $info['validate'] ) ) {
1394 $validate = $info['validate'];
1396 $validator = new XMPValidate( $this->logger
);
1397 $validate = [ $validator, $info['validate'] ];
1400 if ( is_callable( $validate ) ) {
1401 call_user_func_array( $validate, [ $info, &$val, true ] );
1402 // the reasoning behind using &$val instead of using the return value
1403 // is to be consistent between here and validating structures.
1404 if ( is_null( $val ) ) {
1405 $this->logger
->info(
1406 __METHOD__
. " <$ns:$tag> failed validation.",
1407 [ 'file' => $this->filename
]
1413 $this->logger
->warning(
1414 __METHOD__
. " Validation function for $finalName (" .
1415 $validate[0] . '::' . $validate[1] . '()) is not callable.',
1416 [ 'file' => $this->filename
]
1421 if ( $this->ancestorStruct
&& $this->processingArray
) {
1422 // Aka both an array and a struct. ( self::MODE_BAGSTRUCT )
1423 $this->results
['xmp-' . $info['map_group']][$this->ancestorStruct
][][$finalName] = $val;
1424 } elseif ( $this->ancestorStruct
) {
1425 $this->results
['xmp-' . $info['map_group']][$this->ancestorStruct
][$finalName] = $val;
1426 } elseif ( $this->processingArray
) {
1427 if ( $this->itemLang
=== false ) {
1429 $this->results
['xmp-' . $info['map_group']][$finalName][] = $val;
1432 $this->results
['xmp-' . $info['map_group']][$finalName][$this->itemLang
] = $val;
1435 $this->results
['xmp-' . $info['map_group']][$finalName] = $val;